const mongoose=require('mongoose');
const Product=require('../models/product.model')

exports.create=function(req,res,next){
    const product=new Product(req.body);
    product.save().then(data=>{
        res.json(data);
    });
}


exports.update=function(req,res,next){
   var id=req.params.id;
   console.log(id)
   Product.findByIdAndUpdate(id,{$set:req.body},{new:false}).then(data=>{
        res.json(data);
    })
}


exports.get=function(req,res,next){
    var id=req.params.id;
    Product.findById(id).then(data=>{
         res.json(data)
     })
 }

 exports.remove=function(req,res,next){
    var id=req.params.id;
    Product.findByIdAndDelete(id).then(data=>{
         res.json(data)
     })
 }

 exports.removes=function(req,res,next){
    var ids=req.body.ids.split(',');
    Product.remove({_id:{$in:ids}}).then(data=>{
         res.json({"message":"删除多个记录成功",status:200})
     })
 }

 exports.list=function(req,res,next){
     var page=req.body.page?req.body.page:1;
     var rows=req.body.rows?req.body.rows:5;
     Product.paginate({},{page:+page,limit:+rows},function(err,result){
        result.rows=result.docs;
        delete result.docs;
        res.json(result)
    })
 }